package com.bsu.ascm.operation;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.bsu.ascm.R;
import com.bsu.ascm.controls.ListViewTable;
import com.bsu.ascm.controls.ListViewTable.ListViewColumn;
import com.bsu.ascm.http.AscmHttpClient;
import com.bsu.ascm.tool.AscmEnvironment;

public class ReceiveKanban extends Activity {
//	private GridViewTable gvt_table;															//数据表格控件
	private ListViewTable lvt = null;															//ListView表格
	private AscmHttpClient http = new AscmHttpClient();											//http请求
	private XmlPullParserFactory xpp_factory = null;											//xmlpull工厂
	private XmlPullParser xpp = null;															//xmlpull解析对象
	
	//private ArrayList<HashMap<String,String>> srcTable = null;
	//private static HashMap<String,String> colmap = new HashMap<String,String>();				//列内容
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);						//设置横屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);											//无title  
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,    					//全屏
                       WindowManager.LayoutParams. FLAG_FULLSCREEN);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.receivekanban);
		
		init();
		//dataRequest();
	}
	/**
	 *	初始化 ListViewTable版 
	 */
	private void init(){
		//初始化xml解析
		try{
			xpp_factory = XmlPullParserFactory.newInstance();
			xpp = xpp_factory.newPullParser();
		} catch(XmlPullParserException e){
			e.printStackTrace();
		}

		//准备表头数据
		List<ListViewColumn> cols = new ArrayList<ListViewColumn>();
		int avgwidth = AscmEnvironment.getInstance().getDisplayWidth()/4;
		cols.add(new ListViewColumn("NO","编号",avgwidth+70));
		cols.add(new ListViewColumn("TONO","收货单位",avgwidth-20));
		cols.add(new ListViewColumn("REQTIME","发货时间",avgwidth));
		cols.add(new ListViewColumn("ROWQTY","条数",avgwidth-60));
		//准备表格数据
		final List<HashMap<String,String>> tabledata = doRequest();
		
		lvt = new ListViewTable(this);
		lvt.makeTable(this, cols, tabledata);
		lvt.setOnItemClickListener(new AdapterView.OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,
					long arg3){
				//Toast.makeText(ReceiveKanban.this,  String.valueOf(arg2)+"行",500).show();
				if(arg2==0)												//选中标题行时无响应
					return;
				Intent intent = new Intent(ReceiveKanban.this,com.bsu.ascm.operation.ConfirmKanban.class);
				Bundle bl = new Bundle();
				bl.putString("NO", tabledata.get(arg2-1).get("NO"));
				bl.putString("REQTIME", tabledata.get(arg2-1).get("REQTIME"));
				bl.putString("TONO", tabledata.get(arg2-1).get("TONO"));
				intent.putExtras(bl);
				ReceiveKanban.this.startActivity(intent);
			}
		});
		
		LinearLayout ll_rk_table = (LinearLayout)this.findViewById(R.id.ll_rk_table);
		ll_rk_table.addView(lvt);
	}
	
	private List<HashMap<String,String>> doRequest(){
		List<HashMap<String,String>> rettable = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> sortmap = null;
		String xml = "";
		try{
			xml = http.query("mobile_ReceiveKanban","");
			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();
			while(eventType!=XmlPullParser.END_DOCUMENT){
				String nodeName = xpp.getName();
				switch(eventType){
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if("Table".equals(nodeName))
						sortmap = new HashMap<String,String>();
					else if("NO".equals(nodeName))
						sortmap.put("NO", xpp.nextText());
					else if("REQTIME".equals(nodeName))
						sortmap.put("REQTIME", xpp.nextText());
					else if("BATCHNO".equals(nodeName))
						sortmap.put("BATCHNO", xpp.nextText());
					else if("TONO".equals(nodeName))
						sortmap.put("TONO", xpp.nextText());
					else if("ROWQTY".equals(nodeName))
						sortmap.put("ROWQTY", xpp.nextText());
					break;
				case XmlPullParser.END_TAG:
					if("Table".equals(nodeName)){
						//增加每列数据
						rettable.add(sortmap);
					}
					break;
				default:
					break;
				}
				eventType = xpp.next();
			}
		} catch(ClientProtocolException e){
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return rettable;
	}
	
	/**
	 * 初始化 GridViewTable版
	 */
//	private void init(){
//		//初始化xml解析
//		try{
//			xpp_factory = XmlPullParserFactory.newInstance();
//			xpp = xpp_factory.newPullParser();
//		} catch(XmlPullParserException e){
//			e.printStackTrace();
//		}
//		
//		//初始化表格
//		gvt_table = new GridViewTable(this);
//		gvt_table.setTableRowCount(5);															//设置行数
//		LinearLayout ll_rk_table = (LinearLayout) findViewById(R.id.rk_table);
//		gvt_table.setTableOnClickListener(new GridViewTable.OnTableClickListener() {
//			@Override
//			public void onTableClickListener(int x, int y) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//		dataRequest();
//		ll_rk_table.addView(gvt_table);
//		
//	}
//	/**
//	 * 数据请求
//	 */
//	private void dataRequest(){
//		String xml = "";
//		HashMap<String,String> sortmap = new HashMap<String,String>();
//		try{
//			xml = http.query("mobile_ReceiveKanban","");
//			xpp.setInput(new StringReader(xml));
//			int eventType = xpp.getEventType();
//			while(eventType!=XmlPullParser.END_DOCUMENT){
//				String nodeName = xpp.getName();
//				switch(eventType){
//				case XmlPullParser.START_DOCUMENT:
//					srcTable = gvt_table.getSrcTable();
//					srcTable.clear();												//清除表格数据源
//					//增加列名数据
//					HashMap<String,String> c1_map = new HashMap<String,String>();
//					c1_map.put("ItemText", NO);
//					HashMap<String,String> c2_map = new HashMap<String,String>();
//					c2_map.put("ItemText", SEND_TIME);
//					HashMap<String,String> c3_map = new HashMap<String,String>();
//					c3_map.put("ItemText", RECEIVE_UNIT);
//					HashMap<String,String> c4_map = new HashMap<String,String>();
//					c4_map.put("ItemText", ROW_COUNT);
//					
//					srcTable.add(c1_map);
//					srcTable.add(c2_map);
//					srcTable.add(c3_map);
//					srcTable.add(c4_map);
//					break;
//				case XmlPullParser.START_TAG:
//					if("Table".equals(nodeName))
//						sortmap.clear();											//当一行数据开始时清理用于排序的sortmap
//					else if("NO".equals(nodeName))
//						sortmap.put("NO", xpp.nextText());
//					else if("REQTIME".equals(nodeName))
//						sortmap.put("REQTIME", xpp.nextText());
//					else if("BATCHNO".equals(nodeName))
//						sortmap.put("BATCHNO", xpp.nextText());
//					else if("TONO".equals(nodeName))
//						sortmap.put("TONO", xpp.nextText());
//					else if("ROWQTY".equals(nodeName))
//						sortmap.put("ROWQTY", xpp.nextText());
//					break;
//				case XmlPullParser.END_TAG:
//					if("Table".equals(nodeName)){
//						//增加每列数据
//						HashMap<String,String> dc1_map = new HashMap<String,String>();
//						dc1_map.put("ItemText", sortmap.get("NO"));
//						HashMap<String,String> dc2_map = new HashMap<String,String>();
//						dc2_map.put("ItemText", sortmap.get("REQTIME"));
//						HashMap<String,String> dc3_map = new HashMap<String,String>();
//						dc3_map.put("ItemText", sortmap.get("TONO"));
//						HashMap<String,String> dc4_map = new HashMap<String,String>();
//						dc4_map.put("ItemText", sortmap.get("ROWQTY"));
//						
//						srcTable.add(dc1_map);
//						srcTable.add(dc2_map);
//						srcTable.add(dc3_map);
//						srcTable.add(dc4_map);
//					}
//					break;
//				default:
//					break;
//				}
//				eventType = xpp.next();
//			}
//		} catch(ClientProtocolException e){
//			e.printStackTrace();
//		} catch (XmlPullParserException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//		gvt_table.loadTable();
//	}
}
